import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # 去警告
import tensorflow as tf

# tf.compat.v1.disable_eager_execution()


def tensor_demo():
    """
    张量的演示
    :return:
    """
    # 1.张量的阶
    s = tf.constant(483)
    v = tf.constant([1.1, 2.2, 3.3])
    m = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    t = tf.constant([[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]])
    print("s：", s)
    print("v：", v)
    print("m：", m)
    print("t：", t)

    # 2.创建固定张量
    a = tf.zeros(shape=[2, 1])
    b = tf.ones(shape=[4, 3])
    print("a：", a)
    print("b：", b)

    # 3.创建随机张量
    c = tf.random.truncated_normal(shape=(3, 2), mean=2, stddev=1,
                                   dtype=tf.float32)  # 创建一个截断正态分布,返回一个截断的正态分布，截断标准是标准差的二倍
    d = tf.random.normal(shape=(3, 2), mean=2, stddev=1, dtype=tf.float32)  # 创建一个标准正态分布
    e = tf.random.uniform(shape=(2, 3), minval=1, maxval=4, dtype=tf.float32)  # 创建均匀分布张量(注意，均匀不是等差的意思)
    f = tf.random.shuffle(c)  # 随机打乱数组，注意，只打乱第一维元素
    print("c：", c)
    print("d：", d)
    print("e：", e)
    print("f：", f)

    # 4.张量类型变换
    g = tf.cast(m, dtype=tf.float32)
    print("g：", g)

    # 5.张量形状变换
    h = tf.compat.v1.placeholder(dtype=tf.float32, shape=[None, 10])
    print("h：", h)
    i = h.set_shape([2, 10])
    print("i：", h)
    j = tf.reshape(d, shape=[1, 1, 6])
    print("j：", j)

    return None


# 2.x 变量推荐写法
@tf.function
def get_h(x):
    h = 2 * x
    return h


if __name__ == "__main__":
    # 代码6：默认图的演示
    tensor_demo()
